<?php
/**
 * Yotaku
 * Copyright (c) 2016 http://www.yotaku.net All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.net>
 */

class Api {
    //account
    public $account = "cf_zshd";
    public $password = "bjzshd2016";

    public function __construct(){

    }
    public function send($mobile, $mobile_code){
        if(!$mobile) return false;
        if(!$mobile_code) return false;

        //短信接口地址
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

        $post_data = "account={$this->account}&password={$this->password}&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
        //用户名是登录ihuyi.com账号名（例如：cf_demo123）
        //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
        $gets =  xml_to_array($this->Post($post_data, $target));
        if($gets['SubmitResult']['code']==2){
            //success
            return true;
        }else{
            //failed
            return false;
        }
    }

    public function check($mobile, $mobile_code){

        $res = ($mobile_code==session("verification_code"));
        return $res;
    }



    //请求数据到短信接口，检查环境是否 开启 curl init。
    public function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
}